package com.cltech.frame.base.impl;

import com.cltech.frame.base.BaseService;
import com.cltech.frame.base.BaseRepository;
import com.cltech.frame.result.ResponseBean;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;

import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * Created by Administrator on 2018/8/18.
 */
public class BaseServiceImpl<T, ID> implements BaseService<T, ID> {
    private BaseRepository baseRepository;

    public void setBaseRepository(BaseRepository baseRepository) {
        this.baseRepository = baseRepository;
    }

    @Override
    public ResponseBean<List<T>> findAll() {
        return ResponseBean.success(baseRepository.findAll());
    }

    @Override
    public ResponseBean<List<T>> findAll(Sort sort) {
        return ResponseBean.success(baseRepository.findAll(sort));
    }

    @Override
    public ResponseBean<List<T>> findAllById(Iterable<ID> ids) {
        return ResponseBean.success(baseRepository.findAllById(ids));
    }

    @Override
    public <S extends T> ResponseBean<S> save(S entity) {
        return ResponseBean.success((S)baseRepository.save(entity));
    }

    @Override
    public <S extends T> ResponseBean<List<S>> saveAll(Iterable<S> iterables) {
        return ResponseBean.success(baseRepository.saveAll(iterables));
    }

    @Override
    public <S extends T> ResponseBean<S> saveAndFlush(S entity) {
        return ResponseBean.success((S) baseRepository.saveAndFlush(entity));
    }

    @Override
    public void deleteInBatch(Iterable<T> iterables) {
        baseRepository.deleteInBatch(iterables);
    }

    @Override
    public void deleteAllInBatch() {
        baseRepository.deleteAllInBatch();
    }

    @Override
    public ResponseBean<T> getOne(ID id) {
        return ResponseBean.success((T) baseRepository.getOne(id));
    }

    @Override
    public ResponseBean<Optional<T>> findById(ID id) {
        return ResponseBean.success(baseRepository.findById(id));
    }

    @Override
    public <S extends T> ResponseBean<List<S>> findAll(Example<S> example) {
        return ResponseBean.success(baseRepository.findAll(example));
    }

    @Override
    public <S extends T> ResponseBean<List<S>> findAll(Example<S> example, Sort sort) {
        return ResponseBean.success(baseRepository.findAll(example, sort));
    }

    @Override
    public ResponseBean<Optional<T>> findOne(Specification<T> specification) {
        return ResponseBean.success(baseRepository.findOne(specification));
    }

    @Override
    public ResponseBean<List<T>> findAll(Specification<T> specification) {
        return ResponseBean.success(baseRepository.findAll(specification));
    }

    @Override
    public Page<T> findAll(Specification<T> specification, Pageable pageable) {
        return baseRepository.findAll(specification, pageable);
    }

    @Override
    public ResponseBean<List<T>> findAll(Specification<T> specification, Sort sort) {
        return ResponseBean.success(baseRepository.findAll(specification, sort));
    }

    @Override
    public long count(Specification<T> specification) {
        return baseRepository.count(specification);
    }

    @Override
    public <D> ResponseBean<List<D>> listBySQL(Class<D> clazz, String sql, List<Object> values) {
        return ResponseBean.success(baseRepository.listBySQL(clazz, sql, values));
    }

    @Override
    public ResponseBean<List<Map<String, Object>>> listBySQL(String sql, List<Object> values) {
        return ResponseBean.success(baseRepository.listBySQL(sql, values));
    }

    @Override
    public Long countBySQL(String sql, List<Object> values) {
        return baseRepository.countBySQL(sql, values);
    }

    @Override
    public void deleteById(ID id) {
        baseRepository.deleteById(id);
    }

    @Override
    public void delete(T entity) {
        baseRepository.delete(entity);
    }

    @Override
    public void deleteAll(Iterable<? extends T> iterables) {
        baseRepository.deleteAll(iterables);
    }

    @Override
    public void deleteAll() {
        baseRepository.deleteAll();
    }

    @Override
    public boolean existsById(ID id) {
        return baseRepository.existsById(id);
    }

    @Override
    public <S extends T> ResponseBean<Optional<S>> findOne(Example<S> var1) {
        return ResponseBean.success(baseRepository.findOne(var1));
    }

    @Override
    public <S extends T> ResponseBean<Page<S>> findAll(Example<S> var1, Pageable var2) {
        return ResponseBean.success(baseRepository.findAll(var1, var2));
    }

    @Override
    public <S extends T> long count(Example<S> var1) {
        return baseRepository.count(var1);
    }

    @Override
    public <S extends T> boolean exists(Example<S> var1) {
        return baseRepository.exists(var1);
    }
}
